package com.baiduspring.vo;

import com.baiduspring.dao.entity.User;
import com.google.common.collect.Lists;
import com.hankcs.hanlp.HanLP;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 返回数据对象
 *
 * @author xiongyan
 */
@Data
public class ResultsVO {
    private String id;
    private String title;
    private Integer orig;
    private Integer columnChannel;
    private String label;
    private String content;
    private String contentTxt;
    private Long authorAccount;
    private String status;
    private Integer readNum;
    private Integer collectNum;
    private Integer likesNum;
    private Long uriMark;
    private String ip;
    private Long createDate;
    private Long updateDate;
    private String summary;
    private User user;

    private String time;

    public List<String> labels() {
        List<String> list = Lists.newArrayList();
        Arrays.asList(this.label.split(",")).forEach(s -> list.add(s));
        return list;
    }

    public String time() {
        BigDecimal updateDate = new BigDecimal(String.valueOf(Objects.nonNull(this.updateDate) ? this.updateDate : this.createDate));
        BigDecimal currentDate = new BigDecimal(String.valueOf(System.currentTimeMillis()));
        BigDecimal minu = new BigDecimal(String.valueOf(1000 * 60));
        BigDecimal hour = new BigDecimal(String.valueOf(1000 * 60 * 60));

        Long minuV = currentDate.subtract(updateDate).divide(minu, 0, BigDecimal.ROUND_HALF_UP).longValue();
        long hourNum = 60L;
        if (minuV < hourNum) {
            String oneMinBefore = "1分钟前 发布";
            return minuV.equals(0L) ? oneMinBefore : minuV + " 分钟前 发布";
        }

        Long hourV = currentDate.subtract(updateDate).divide(hour, 0, BigDecimal.ROUND_HALF_UP).longValue();
        float dayNum = 24f;
        if (hourV >= dayNum) {
            BigDecimal decimal = new BigDecimal(String.valueOf(hourV));

            Float days = decimal.divide(new BigDecimal("24"), 0, BigDecimal.ROUND_HALF_UP).floatValue();
            float yearNum = 360f;
            if (days >= yearNum) {
                return decimal.divide(new BigDecimal("365"), 0, BigDecimal.ROUND_HALF_UP).longValue() + " 年前 发布";
            }

            return decimal.divide(new BigDecimal("24"), 0, BigDecimal.ROUND_HALF_UP).longValue() + " 天前 发布";
        }

        return hourV + " 小时前 发布";
    }

    public String summary() {
        StringBuilder builder = new StringBuilder();
        HanLP.extractSummary(this.getContentTxt(), 3).forEach(s -> builder.append(s).append("，"));
        builder.deleteCharAt(builder.lastIndexOf("，"));
        return builder.toString();
    }

    public String getTime() {
        return this.time();
    }

    public void setTime(String time) {
        this.time = time;
    }
}
